# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=ocaml-migrate-parsetree
pkgver=1.0.11
pkgrel=1
pkgdesc="Convert OCaml parsetrees between different major versions"
url="https://github.com/ocaml-ppx/ocaml-migrate-parsetree"
arch="all !x86 !armhf !armv7 !s390x !mips !mips64"  # limited by ocaml
license="LGPL-2.1-only-WITH-linking-exception"
depends="ocaml-result ocaml-runtime"
depends_dev="ocaml-result-dev"
makedepends="$depends_dev dune ocaml ocaml-compiler-libs ocaml-findlib
	ocaml-ocamlbuild-dev opam"
options="textrels"  # ppc64le (?!)
subpackages="$pkgname-dev $pkgname-ocamlbuild"
source="$pkgname-$pkgver.tar.gz::https://github.com/ocaml-ppx/$pkgname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$pkgname-$pkgver"

build() {
	cd "$builddir"
	jbuilder build @install
}

check() {
	cd "$builddir"
	jbuilder runtest
}

package() {
	cd "$builddir"

	mkdir -p "$pkgdir"/usr/lib/ocaml
	jbuilder install \
		--destdir="$pkgdir/usr" \
		--libdir="$pkgdir/usr/lib/ocaml"

	cd "$pkgdir"

	find usr/lib/ocaml -name '*.cmxs' -exec chmod 0755 {} \;

	# Remove annotation files, sources and some generated code (?).
	find usr/lib/ocaml \( \
		-name '*.cmt' -o \
		-name '*.cmti' -o \
		-name '*.ml' -o \
		-name '*.ml-gen' \) -delete

	# There's just a readme, changelog etc.
	rm -Rf usr/doc
}

ocamlbuild() {
	pkgdesc="$pkgdesc (ocamlbuild plugin)"
	depends="$pkgname=$pkgver-r$pkgrel ocaml-ocamlbuild"

	mkdir -p "$subpkgdir"/usr/lib/ocaml
	mv "$pkgdir"/usr/lib/ocaml/$pkgname-ocamlbuild "$subpkgdir"/usr/lib/ocaml/
}

dev() {
	default_dev

	depends="$depends_dev
		$pkgname=$pkgver-r$pkgrel
		$pkgname-ocamlbuild=$pkgver-r$pkgrel"

	cd "$pkgdir"

	local path; for path in $(find usr/lib/ocaml/$pkgname*/ \
		-name '*.cmx' -o \
		-name '*.cmxa' -o \
		-name '*.mli')
	do
		mkdir -p "${path%/*}"
		mv "$path" "$subpkgdir"/${path%/*}/
		rmdir "$path" 2>/dev/null || true
	done
}

sha512sums="37d877ab00e6ff6c93916da49a6bdeb0af16f4ce9cc32fe1c41438c7e344bc5a5400432f11edc44b73c8308bc509d0b393344f9d2a626677a658801655276821  ocaml-migrate-parsetree-1.0.11.tar.gz"
